var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addbankcard:true,
    bind:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    //角色切换
    util.getRoleList(this, app.globalData.CO_ID, app.globalData.role_default, app.globalData.roleData);

    wx.showLoading({
      title: '获取绑定信息中...',
    });
    util.getData("/atapi/user/user/bindbankcard", {
      type: (app.globalData.role_default == 'admin' || app.globalData.role_default == 'operation')? 'company' : 'person',
      vehicleId: app.globalData.vehicleId,
      CO_ID: app.globalData.CO_ID,
      user_id: app.globalData.user_id
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        return false;
      }
      if (res.data.success) {
        console.log(res.data.data)
        var data = res.data.data[0];
        that.setData({
          bind:true,
          addbankcard: false,   
          name: data.name,
          bank: data.bank,
          bankcard: data.bankcard,
          idcard: data.idcard,
          location: data.location,
          account_subject: data.account_subject,
          operation_user: data.operation_user
        })
      } else {
        that.setData({
          account_subject: (app.globalData.role_default == 'admin' || app.globalData.role_default == 'operation') ? app.globalData.CO_NAME : app.globalData.plate,
          operation_user: app.globalData.truename
        })
      }
    });
    
  },
  bankSuccess:function(e){
    var that = this;
     console.log(e)
     var card = e.detail.number;
     console.log(card)
    wx.showLoading({
      title: '请稍候...',
    });
    util.getData("/atapi/user/user/bankcard", {
      card: card
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        console.log(res.data.data)
        var data = JSON.parse(res.data.data);
        if (data.msg == "ok"){
          that.setData({
            addbankcard:false,
            bank: data.result.bank,
            bankcard: data.result.bankcard,
            //location: data.result.province + (data.result.city ? '，' + data.result.city:'')
          })
        }else{
          wx.showToast({
            title: '没该卡号信息',
            image: '/images/wrong.png',
            duration: 3000
          });
          that.setData({
            addbankcard: false,
            bankcard: card
          })
        }
      }else{
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  reAddBankCard:function(){
    this.setData({
      addbankcard: true
    });
  },
  saveBankCard: function () {
    var that = this;
    var bank = that.data.bank || '', idcard = that.data.idcard || '', bankcard = that.data.bankcard||'', location = that.data.location||'';
    var account_subject = that.data.account_subject||'', operation_user = that.data.operation_user;
    var CO_ID = app.globalData.CO_ID,user_id=app.globalData.user_id;
    var name = that.data.name || '';
    var vehicleId = app.globalData.vehicleId;
    if (name == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有开户名，请输入开户人姓名或企业名称',
        success: function (res) {

        }
      })
      return false;
    }
    if (bank == ''){
      wx.showModal({
        title: '温馨提示',
        content: '没有银行名称信息，请重新上传或输入银行名称',
        success: function (res) {
          
        }
      })
      return false;
    }
    if (bankcard == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有银行卡号信息，请重新上传或输入银行卡号',
        success: function (res) {

        }
      })
      return false;
    }
    if (location == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有开户行所在地信息，请输入XX分行XX支行',
        success: function (res) {

        }
      })
      return false;
    }
    if (idcard == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有身份证号码信息',
        success: function (res) {

        }
      })
      return false;
    }
    if (account_subject == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有银行卡绑定主体信息，请重新登录小程序。尝试后还是没有，请联系相关负责人',
        success: function (res) {

        }
      })
      return false;
    }
    util.postData("/atapi/user/user/bankcard", {
      name: name,
      bank: bank,
      bankcard: bankcard,
      idcard: idcard,
      location: location,
      account_subject: account_subject,
      operation_user: operation_user,
      vehicleId: vehicleId,
      CO_ID: CO_ID,
      user_id: user_id
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        console.log(res.data.data)
        wx.showModal({
          title: '温馨提示',
          content: '绑定银行卡设置成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/auth/bank_account'
            })
          }
        })
      } else {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})